﻿namespace Typps
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Web.Script.Serialization;
    using System.Web.UI.WebControls;

    /// <summary>
    /// This converter knows how to serialize following types :
    /// List<ToolBarItem>, TabCollection,DictionaryCollection and CssClassNameCollection
    /// The main reason for this converter is also to follow a pascal casing convention
    /// when serializing the types. Unfortunately, there is no better way to map objects using the
    /// builtin JavaScriptSerializer. On the bright side, this is not so bad, also because it does not
    /// make use of reflection, we get a negligable performance boost.
    /// NOTE: currently deserialization is not supported via this converter since it's not required.
    /// </summary>
    public class EditorJavaScriptConverter : JavaScriptConverter
    {
        #region Properties

        public override IEnumerable<Type> SupportedTypes
        {
            //Define the ListItemCollection as a supported type.
            get
            {
                return new ReadOnlyCollection<Type>(new List<Type>(
                 new Type[] {
                    typeof(ToolBarCollection),
                    typeof(TabCollection),
                    typeof(DictionaryCollection),
                    typeof(CssClassNameCollection)
                 } ));
            }
        }

        #endregion Properties

        #region Methods

        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            if (dictionary == null)
                throw new ArgumentNullException("dictionary");
            //Does not support deserializing. In effect we don't need to deserialize. YAGNI principle?
            return null;
        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            ToolBarCollection toolBarList = obj as ToolBarCollection;
            TabCollection tabList = obj as TabCollection;
            DictionaryCollection dicList = obj as DictionaryCollection;
            CssClassNameCollection cssClassNameList = obj as CssClassNameCollection;
            Dictionary<string, object> result = null;
            ArrayList itemsList = null;
            if (toolBarList != null)
            {
                result = new Dictionary<string, object>();
                for(int num1=0;num1<toolBarList.Count;num1++)
                {
                    itemsList = new ArrayList();
                    ArrayList itemsList2 = new ArrayList();
                    ToolBarSet tbs=toolBarList[num1];
                    Dictionary<string, object> tbsDict = new Dictionary<string, object>();
                    foreach (ToolBarItem tbi in tbs.Items)
                    {
                        Dictionary<string, object> itemsDict = new Dictionary<string, object>();
                        itemsDict.Add("marginLeft", tbi.MarginLeft);
                        itemsDict.Add("command", tbi.Command);
                        itemsDict.Add("title", tbi.ToolTip);
                        itemsDict.Add("iconUrl", tbi.Icon);
                        itemsDict.Add("divider", tbi.Break);
                        itemsDict.Add("enabled", tbi.Enabled);
                        itemsList.Add(itemsDict);
                    }
                    tbsDict.Add("index", num1);
                    tbsDict.Add("leftBorderImage", tbs.LeftBorderImage);
                    tbsDict.Add("rightBorderImage", tbs.RightBorderImage);
                    tbsDict.Add("items", itemsList);
                    itemsList2.Add(tbsDict);
                    result.Add(string.Format("toolBarSet{0}", num1), itemsList2);
                }
                return result;
            }
            else if (tabList != null)
            {
                result = new Dictionary<string, object>();
                itemsList = new ArrayList();
                foreach (TabItem ti in tabList)
                {
                    Dictionary<string, object> itemsDict = new Dictionary<string, object>();
                    itemsDict.Add("marginLeft", ti.MarginLeft);
                    itemsDict.Add("command", ti.Command);
                    itemsDict.Add("title", ti.ToolTip);
                    itemsDict.Add("width", ti.Width.Value);
                    itemsDict.Add("iconUrl", ti.Icon);
                    itemsDict.Add("divider", ti.Break);
                    itemsDict.Add("enabled", ti.Enabled);
                    itemsList.Add(itemsDict);
                }
                result["items"] = itemsList;
                return result;
            }
            else if (dicList != null)
            {
                result = new Dictionary<string, object>();
                itemsList = new ArrayList();
                foreach (DictionaryItem di in dicList)
                {
                    Dictionary<string, object> itemsDict = new Dictionary<string, object>();
                    itemsDict.Add("text", di.Text);
                    itemsDict.Add("value", di.Value);
                    itemsDict.Add("selected", di.Selected);
                    itemsList.Add(itemsDict);
                }
                result["dictionaries"] = itemsList;
                return result;
            }
            else if (cssClassNameList != null)
            {
                result = new Dictionary<string, object>();
                itemsList = new ArrayList();

                foreach (CssClassName ccn in cssClassNameList)
                {
                    Dictionary<string, object> itemsDict = new Dictionary<string, object>();
                    itemsDict.Add("name", ccn.ClassName);
                    itemsDict.Add("value", ccn.Value);
                    itemsDict.Add("title", ccn.ToolTip);
                    itemsList.Add(itemsDict);
                }
                result["cssClassNameCollection"] = itemsList;
                return result;
            }
            return new Dictionary<string, object>();
        }

        #endregion Methods
    }
}